// components/scribemes/scribemes.js
const util = require('../../utils/util.js')
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    mobile:'',  //手机号码
    authorizeScribeMes: false
  },
  /*  */
  lifetimes: {
    attached: function () {
      this.setData({
        authorizeScribeMes: app.globalData.authorizeScribeMes
      })
    }
  },
  /*  */
  pageLifetimes:{
    show: function(){
      console.log(app.globalData.authorizeScribeMes)
      this.setData({
        authorizeScribeMes: app.globalData.authorizeScribeMes
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    SubscribeMessage: function () {
      new Promise((resolve, reject) => {
        wx.requestSubscribeMessage({
          tmplIds: [app.globalData.subScribeMessageCode], // 此处可填写多个模板 ID，但低版本微信不兼容只能授权一个
          success: res => {
            //console.log(res)
            if (res[app.globalData.subScribeMessageCode] == 'accept') {
              app.globalData.authorizeScribeMes = true
              this.setData({
                authorizeScribeMes: app.globalData.authorizeScribeMes
              })
              resolve()
            } else {
              reject(res)
            }
          },
          fail: err => {
            console.log(err)
            reject(err)
          }
        })
      }).then(() => {
        //调登录接口
        wx.showLoading({
          title: '',
          mask:true
        })
        this.user_loginWx().then(() => {
          //判断是否有手机号码
          if(!app.globalData.mobile){
            wx.showModal({
              title:'提示',
              content:'暂未绑定手机号码，请绑定',
              showCancel:false,
              confirmColor:app.globalData.modalConfirmColor,
              success: res=>{
                if(res.confirm){
                  wx.switchTab({
                    url:'/pages/personal/personal'
                  })
                }
              }
            })
          }
        }).catch(errMsg => {
          wx.showModal({
            title: '提示',
            content:`${errMsg}`,
            showCancel: false,
            confirmColor: app.globalData.modalConfirmColor
          })
        }).finally(() => {
          wx.hideLoading()
        })
      }).catch(err => {
        wx.showModal({
          title: '提示',
          content: '请授权开启订阅消息',
          showCancel: false,
          confirmColor: app.globalData.modalConfirmColor
        })
      })
    },
    /* 获取登录用户的后台信息 */
    user_loginWx: function () {
      return new Promise((resolve, reject) => {
        wx.request({
          url: app.globalData.Url + '/loginWxCar',
          method: 'POST',
          header: {
            'content-type': app.globalData.contentType
          },
          data: util.json2Form({
            avatar: app.globalData.userInfo.avatar,  //头像
            mobile: app.globalData.mobile,  //手机号码
            nickName: app.globalData.userInfo.nickName,  //昵称
            openid: app.globalData.openid,    //openid
          }),
          success: res => {
            console.log(res)
            app.globalData.token = res.data.body.token
            app.globalData.header.cartoken = res.data.body.token
            app.globalData.userid = res.data.body.userid
            app.globalData.mobile = res.data.body.mobile?res.data.body.mobile:''
            //console.log(app.globalData)
            resolve()
          },
          fail: err => {
            reject('获取与登录用户对应的后台信息失败')
          }
        })
      })
    },
  }
})
